package cn.ctbu.xiaopeng.studentscoremanage.api;

import cn.ctbu.xiaopeng.studentscoremanage.enums.ResultEnum;
import cn.ctbu.xiaopeng.studentscoremanage.exception.ResultException;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.dto.OperateLogDTO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.dto.PageQueryDTO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.dto.StudentQueryDTO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.entity.OperateLog;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.entity.Student;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.OperateLogVO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.StudentPageVO;
import cn.ctbu.xiaopeng.studentscoremanage.pojo.vo.page.PageResult;
import cn.ctbu.xiaopeng.studentscoremanage.service.OperateLogService;
import cn.ctbu.xiaopeng.studentscoremanage.utils.BuildTbSortUtils;
import cn.ctbu.xiaopeng.studentscoremanage.utils.PageResultUtils;
import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author xiaopeng
 * @version 1.0
 */
@RestController
@RequestMapping("/api/operateLog")
@RequiredArgsConstructor
@Slf4j
public class OperateLogApiController {
    private final OperateLogService operateLogService;

    /**
     * 分页查询日志信息,可以实现按照表头排序
     *
     * @param pageQueryDTO
     * @return
     */
    @PostMapping("/page")
    public PageResult getAllStuByPageWithSort(@RequestBody PageQueryDTO<OperateLogDTO> pageQueryDTO) {

        // 默认分页参数
        int pageIndex = 0;
        int pageSize = 10;

        if (pageQueryDTO == null) {
            // pageQueryVO为空直接走默认分页查询
            Pageable pageable = PageRequest.of(pageIndex, pageSize);
            Page<OperateLog> operateLogs = operateLogService.getAllWithPage(pageable); // 使用默认分页和排序
            return PageResultUtils.success(operateLogs.getContent(), operateLogs.getTotalElements());
        } else {
            // 根据传入的页码和页面大小初始化分页参数
            pageIndex = pageQueryDTO.getPage() > 0 ? pageQueryDTO.getPage() - 1 : 0;
            pageSize = pageQueryDTO.getLimit();

            // 初始化排序对象
            Sort sort = Sort.unsorted();
            if (pageQueryDTO.getSorts() != null && !pageQueryDTO.getSorts().isEmpty()) {
                // 构建Sort对象，根据前端传入的多个排序参数
                sort = BuildTbSortUtils.buildSort(pageQueryDTO.getSorts());
            } else {
                // 如果没有排序规则，则默认按照id升序排序
                sort = Sort.by(Sort.Direction.ASC, "id");
            }

            // 创建Pageable对象，包含分页和排序信息
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

            if (pageQueryDTO.getData() != null) {
                // 根据传入的学生信息进行查询
                OperateLogDTO operateLogDTO = pageQueryDTO.getData();

                Page<OperateLogVO> operateLogVOS = null;

                if (pageQueryDTO.getData().getOperateTimeBegin() != null && pageQueryDTO.getData().getOperateTimeEnd() != null) {
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

                    LocalDateTime operateTimeBegin = LocalDateTime.parse(pageQueryDTO.getData().getOperateTimeBegin(), formatter);
                    LocalDateTime operateTimeEnd = LocalDateTime.parse(pageQueryDTO.getData().getOperateTimeEnd(), formatter);
                    // 执行查询
                    operateLogVOS =
                            operateLogService.getOperateLogsByConditions(operateLogDTO, operateTimeBegin, operateTimeEnd, pageable);
                } else {
                    // 执行查询
                    operateLogVOS =
                            operateLogService.getOperateLogsByConditions(operateLogDTO, null, null, pageable);
                }


                // 返回分页结果
                return PageResultUtils.success(BeanUtil.copyToList(operateLogVOS.getContent(), OperateLogVO.class),
                        operateLogVOS.getTotalElements());
            } else {
                // 如果pageQueryVO不为空，但data为空，则抛出异常
                throw new ResultException(ResultEnum.COMMON_ERROR);
            }
        }
    }

}
